package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.edgclub.edg.R;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.model.RecommendTopic;
import com.edgclub.edg.model.result.FindCartoonData;
import com.edgclub.edg.uc.UnableScrollGridView;
import com.edgclub.edg.uc.UnableScrollListView;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DensityUtils;

import java.util.ArrayList;


/**
 */
public class FindCartoonfragmentAdapter extends GzBaseAdapter{
    private Context mContext;
    private GlideUtils glideUtils;
    private int width;

    public static final int cartoon_stars = 0;
    public static final int cartoon_topics = 1;

    private FindCartoonStarAdapter cartoonStarAdapter;
    private FindCartoonAdapter cartoonAdapter;
    private ArrayList<RecommendTopic> datas;
    private ArrayList<FindCartoonData.CartoonStar> stars;

    public FindCartoonfragmentAdapter(Context context) {
        this.mContext = context;
        glideUtils = new GlideUtils();
        width = (DensityUtils.getScreenW((Activity) mContext)-(int) mContext.getResources().getDimension(
                R.dimen.size_30))/2;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(viewType == cartoon_stars){
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_find_cartoon_fragment,viewGroup,false);
            return new ViewHolder(view,mListener,mLongClickListener);
        }else{
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_find_cartoon_fragment2,viewGroup,false);
            return new TopicViewHolder(view,mListener,mLongClickListener);
        }

    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder1, final int position) {
        switch (getItemViewType(position)){
            case cartoon_stars:
                ViewHolder viewHolder = (ViewHolder) viewHolder1;
                if(stars!=null && stars.size()>0){
                    cartoonStarAdapter = new FindCartoonStarAdapter(mContext);
                    cartoonStarAdapter.setDatas(stars);
                    viewHolder.listView.setAdapter(cartoonStarAdapter);
                }
                break;
            case cartoon_topics:
                if(datas!=null && datas.size()>0){
                    TopicViewHolder topicViewHolder = (TopicViewHolder) viewHolder1;
                    cartoonAdapter = new FindCartoonAdapter(mContext);
                    cartoonAdapter.setDatas(datas);
                    topicViewHolder.unableScrollGridView.setAdapter(cartoonAdapter);
                }
                break;
        }

    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        int size = 0;
        if(stars!=null && stars.size()>0){
            size = size+1;
        }
        if(datas!=null && datas.size()>0){
            size = size+1;
        }
        return size;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return cartoon_stars;
            case 1:
                return cartoon_topics;
        }
        return cartoon_stars;
    }

    public ArrayList<RecommendTopic> getDatas() {
        return datas;
    }

    public void setDatas(ArrayList<RecommendTopic> datas) {
        this.datas = datas;
    }

    public ArrayList<FindCartoonData.CartoonStar> getStars() {
        return stars;
    }

    public void setStars(ArrayList<FindCartoonData.CartoonStar> stars) {
        this.stars = stars;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private UnableScrollListView listView;

        public ViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            listView = (UnableScrollListView) view.findViewById(R.id.recyclerview);

        }
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class TopicViewHolder extends RViewHolder {
        private UnableScrollGridView unableScrollGridView;

        public TopicViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            unableScrollGridView = (UnableScrollGridView) view.findViewById(R.id.unableScrollGridView);

        }
    }





}
